package com.shundr.common.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shundr.R;
import com.shundr.common.model.AreaInfo;
import com.shundr.common.view.OptimizeGridView.OptimizeGridAdapter;

/**
 * 
 * GridAdapter
 * 
 * @author larry 2015-6-3 下午4:05:19 
 * @version 1.0.0
 *
 */
public class AreaAdapter extends BaseAdapter implements OptimizeGridAdapter<AreaInfo> {

	private List<AreaInfo> mList = new ArrayList<AreaInfo>();
	private Context mContext;

	public AreaAdapter(Context context, List<AreaInfo> list) {
		this.mContext = context;
		this.mList = list;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.item_area_grid, null);
		}
		TextView text = (TextView) convertView.findViewById(R.id.tv_name);
		AreaInfo item = (AreaInfo) getItem(position);
		// 解决残影问题，这里判断如果是NULL item则只显示一个白色背景
		if (isNullItem(item)) {
			// convertView.setBackgroundResource(R.drawable.grid_bg);
			text.setVisibility(View.INVISIBLE);
			return convertView;
		}
		text.setVisibility(View.VISIBLE);
		// convertView.setBackgroundResource(R.drawable.grid_item_border);
		text.setText(item.getName());
		return convertView;
	}

	public static AreaInfo NULL_ITEM = new AreaInfo();

	@Override
	public List<AreaInfo> getItems() {
		return mList;
	}

	@Override
	public void setItems(List<AreaInfo> items) {
		mList = items;
		notifyDataSetChanged();
	}

	@Override
	public AreaInfo getNullItem() {
		return NULL_ITEM;
	}

	@Override
	public boolean isNullItem(AreaInfo item) {
		return item == NULL_ITEM;
	}

}
